package com.GoldMiner.ui;

import com.GoldMiner.util.DataBase;

import javax.swing.*;
import java.awt.*;

import static com.GoldMiner.util.Img.loginbg;
import static com.GoldMiner.util.Img.logo;

public class Login extends JFrame {
    /**
     * 构造一个登录窗口对象
     *
     * @throws Exception 如果在创建登录窗口对象时发生异常
     */
    public Login() throws Exception {
        //数据库
        DataBase db = new DataBase();
        //设置窗口属性
        this.setTitle("用户账号登录");
        this.setIconImage(logo);
        //设置字体
        this.setFont(new Font("幼圆", Font.BOLD, 18));
        //设置布局
        this.setLayout(null);
        this.setSize(900,600);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //面板背景
        //ImageIcon loginbg = new ImageIcon("src/com/GoldMiner/image/login.jpg");
        JLabel loginlabel = new JLabel(loginbg);
        loginlabel.setBounds(0, 0, 900, 600);
        this.setBackground(new Color(0xffffff));
        //用户名输入框
        JLabel usernameLabel = new JLabel("用户名：");
        usernameLabel.setFont(new Font("幼圆", Font.BOLD, 18));//设置字体
        JTextField usernameField = new JTextField(20);
        usernameField.setFont(new Font("幼圆", Font.BOLD, 18));//设置字体
        //密码输入框
        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setFont(new Font("幼圆", Font.BOLD, 18));//设置字体
        JPasswordField passwordField = new JPasswordField(20);
        passwordField.setFont(new Font("幼圆", Font.BOLD, 18));//设置字体
        //登录按钮
        JButton loginButton = new JButton("登录");
        JButton registerButton = new JButton("注册");
        //设置字体为幼圆
        loginButton.setFont(new Font("幼圆", Font.BOLD, 18));
        registerButton.setFont(new Font("幼圆", Font.BOLD, 18));

        // 创建自定义面板来放置用户名和密码组件
        JPanel loginPanel = new JPanel(null) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                // 绘制背景图片
                g.drawImage(loginbg.getImage(), 0, 0, getWidth(), getHeight(), this);
            }
        };
        loginPanel.setBounds(0, 0, 900, 600);
        loginPanel.setOpaque(true); // 设置为true以确保背景色可见

        //设置按钮位置
        loginButton.setBounds(300, 420, 100, 30);
        registerButton.setBounds(500, 420, 100, 30);

        //设置用户名输入框位置
        usernameLabel.setBounds(250, 240, 80, 30);
        usernameField.setBounds(320, 240, 300, 40);

        //设置密码输入框位置
        passwordLabel.setBounds(270, 340, 80, 30);
        passwordField.setBounds(320, 340, 300, 40);

        // 添加组件到loginPanel
        loginPanel.add(usernameLabel);
        loginPanel.add(usernameField);
        loginPanel.add(passwordLabel);
        loginPanel.add(passwordField);
        loginPanel.add(loginButton);
        loginPanel.add(registerButton);

        // 添加loginPanel到窗口
        this.add(loginPanel);

        this.setVisible(true);

        //设置成功弹窗
        //.setResizable(false);
        JDialog dialoge = new JDialog(this, "登录成功", false);
        dialoge.setSize(400, 200);
        dialoge.setLocationRelativeTo(null);
        dialoge.setLayout(null);
        JLabel successLabel = new JLabel("登录成功！欢迎游玩重庆师范大学版黄金矿工！");
        successLabel.setFont(new Font("幼圆", Font.BOLD, 15));
        successLabel.setBounds(10, 10, 380, 100);
        dialoge.add(successLabel);
        //登录按钮事件
        loginButton.addActionListener(e -> {
            String username = usernameField.getText();
            String password = new String(passwordField.getPassword());
            System.out.println("用户名:" + username + " password:" + password);
            System.out.println(db.getPassword(username));
            if (password.equals(db.getPassword(username))) {
                dialoge.setVisible(true);
                db.setUsername(username);
                db.setPassword(password);
                new GameMain(db);
                this.dispose();//关闭登录窗口
            } else {
                JOptionPane.showMessageDialog(this, "用户名或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });
        //注册按钮事件
        registerButton.addActionListener(e -> {
            try {
                new Register();
                this.dispose();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        //可视
        this.setVisible(true);
    }
}

